from django.shortcuts import render,HttpResponse,redirect

# Create your views here.
from rbac import models
from rbac.service import permissions
import json

def login(request):
    if request.method == "POST":
        user = request.POST.get("user")
        ppwd = request.POST.get("pwd")
        from rbac.ldap_pam_auth import ldap_auth
        ret = ldap_auth(user,ppwd)
        if ret[0]:
            username = ret[1]
            user = models.User.objects.filter(sAMAccountName=username).first()
            print("xxxxxxxxx",user)
        else:
            user = None
        if user:
            request.session["user_id"] = user.pk
            print(user)
            permissions.initial_session(user, request)
            return redirect("/")
    return render(request,"rbac/login.html")

def logout(request):
    request.session.flush()
    return redirect("/rbac/login/")

def reset(request):
    user_list = models.User.objects.all()
    id=request.session.get("user_id")
    user=models.User.objects.filter(id=id).first()
    print("111111111111")
    print(request.session["menu_permission_list"])
    per=Per(request.actions)
    return render(request,"rbac/reset.html",locals())


class Per(object):
    def __init__(self,actions):
        self.actions=actions
    def add(self):
        return "add" in self.actions
    def delete(self):
        return "delete" in self.actions
    def edit(self):
        return "edit" in self.actions
    def list(self):
        return "list" in self.actions

def users(request):
    search_name = request.GET.get("username")
    print("jjjjj",search_name)
    if search_name:
        search_name = search_name.strip()
    else:
        search_name = ""
    user_list = models.User.objects.filter(displayName__contains=search_name).all()
    id=request.session.get("user_id")
    user=models.User.objects.filter(id=id).first()
    print("22222222222")
    print(user.department.id)
    print(request.session["menu_permission_list"])
    per=Per(request.actions)
    print(user.department.name)
    return render(request,"users.html",locals())

# from rbac import models as rbac_models
def userinfo(request):
    if request.method == "POST":
        pass
    roles = list(models.Role.objects.all().values_list("title"))
    print("roles",roles)
    return HttpResponse(json.dumps(roles))

def user_add(request):
    return HttpResponse("user_add")


def user_delete(request,id):
    return HttpResponse("delete",id)


def user_edit(request,id):
    return HttpResponse("edit",id)


def roles(request):
    role_lists = models.Role.objects.all()
    user_list = models.User.objects.all()
    id=request.session.get("user_id")
    user=models.User.objects.filter(id=id).first()
    print("22222222222")
    print(request.session["menu_permission_list"])
    per=Per(request.actions)
    return render(request,"roles.html",locals())


def perm_info(request):
    # print(44444)
    res = {"status":500}
    data = []
    for gg in models.PermissionGroup.objects.all():
        print(gg.title)
        print(gg.id)
        gg_dic = dict()
        gg_dic["id"] = gg.id
        gg_dic["title"] = gg.title
        perms = list(models.Permission.objects.filter(permissiongroup__id=gg.id).values("id","title"))
        print(perms)
        gg_dic["perms"] = perms
        data.append(gg_dic)
    print(data)
    return HttpResponse(json.dumps(data))

def add_role(request):
    res = {"status":None}

    return HttpResponse(json.dumps(res))

def del_role(request):
    res = {"status":None}

    return HttpResponse(json.dumps(res))

def edit_role(request,id):
    res = {"status":None}

    return HttpResponse(json.dumps(res))

# a  = "a"
# a.strip()

def permssion(request):
    res = {"status":500}
    id=request.session.get("user_id")
    user=models.User.objects.filter(id=id).first()
    print("22222222222")
    print(request.session["menu_permission_list"])
    per=Per(request.actions)
    permissions = models.Permission.objects.all()
    return render(request,"permission.html",locals())